## Load Balancing Traffic in Neutron (Chapter 7)
## CONTROLLER NODE ONLY!

## Installation ##

yum -y install haproxy

#LinuxBridge Only
crudini --set /etc/neutron/lbaas_agent.ini DEFAULT interface_driver neutron.agent.linux.interface.BridgeInterfaceDriver
# End LinuxBridge Only

#OVS Only
crudini --set /etc/neutron/lbaas_agent.ini DEFAULT interface_driver neutron.agent.linux.interface.OVSInterfaceDriver
# End OVS Only

crudini --set /etc/neutron/lbaas_agent.ini DEFAULT device_driver neutron.services.loadbalancer.drivers.haproxy.namespace_driver.HaproxyNSDriver
crudini --set /etc/neutron/lbaas_agent.ini DEFAULT user_group nobody

# Edit /etc/neutron/neutron.conf and add the following:
crudini --set /etc/neutron/neutron.conf DEFAULT service_plugins neutron.services.loadbalancer.plugin.LoadBalancerPlugin

service neutron-server restart
service neutron-lbaas-agent start
chkconfig neutron-lbaas-agent on
sed -i "/'enable_lb': False,/c\'enable_lb': True," /etc/openstack-dashboard/local_settings
service httpd restart

## Demonstration ##

# Execute the following on WEB1 to start a simple listener:

echo "This is Web1" > ~/index.html
sudo python -m SimpleHTTPServer 80

# Execute the following on WEB2

echo "This is Web2" > ~/index.html
sudo python -m SimpleHTTPServer 80


neutron lb-pool-create --description "The Web Pool" --lb-method ROUND_ROBIN --name WEB_POOL --protocol HTTP --subnet-id $(neutron subnet-list | grep TENANT | awk '{ print $2 }')
neutron lb-member-create --address 10.30.0.2 --protocol-port 80 WEB_POOL
neutron lb-member-create --address 10.30.0.4 --protocol-port 80 WEB_POOL

neutron lb-healthmonitor-create --delay 5 --max-retries 3 --timeout 16 --type TCP
neutron lb-healthmonitor-associate <monitor id> WEB_POOL

neutron lb-vip-create --description "The Web VIP" --name WEB_VIP --protocol-port 80 --protocol HTTP --subnet-id $(neutron subnet-list | grep TENANT | awk '{ print $2 }') WEB_POOL

# Use curl to test HTTP requests against the VIP that gets created

